package com.qiangqiang.taskuser.controller;

import com.alibaba.fastjson.JSON;
import com.qiangqiang.taskuser.entity.UserInfo;
import com.qiangqiang.taskuser.mapper.UserMapper;
import com.qiangqiang.taskuser.service.UserService;
import com.qiangqiang.taskuser.util.ResultVOUtil;
import com.qiangqiang.taskuser.vo.ResultVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
//@RequestMapping("/user")  //在网关配置
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/test")
    public String test() {
        return "OK";
    }

    @GetMapping("/list/{page}/{pageSize}")
    public ResultVO getUserList(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize) {
        return ResultVOUtil.success(userService.getUserList(page, pageSize));
    }

    /**
      * @author sky
      * @date 2018/8/15
      * @param [userId]
      * @return com.qiangqiang.taskuser.vo.ResultVO
      * @describe: 吞吐量：每秒处理 68 个请求
     */
    @GetMapping("/getUserById")
    public ResultVO getUserById(@Param("userId") long userId) {
        return ResultVOUtil.success(userService.getUserById(userId));
    }

    @GetMapping("/getUserByNickName")
    public ResultVO getUserByNickname(@Param("nickName") String nickName) {
        return ResultVOUtil.success(userService.getUserByNickName(nickName));
    }

    @PostMapping("/adduser")
    public ResultVO upSertUser(UserInfo user) {
        return ResultVOUtil.success(userService.upSertUser(user));
    }


    @GetMapping("/removeuser")
    public ResultVO removeUser(@Param("userId") long userId) {
        return ResultVOUtil.success(userService.removeUser(userId));
    }

    @PostMapping("/getNickNames")
    public ResultVO getNicknames(@RequestBody String userIds) {
        return ResultVOUtil.success(userService.getNicknamesByIds(
                JSON.parseArray(userIds, Long.class)));
    }

    @PostMapping("/getNickNameById")
    public ResultVO getNickNameDTOByIds(@RequestBody String userIds) {
        return ResultVOUtil.success(userService.getNickNameDTOByIds(
                JSON.parseArray(userIds, Long.class)));
    }

    /**
     * @Description: 根据 userId查找电话，仅内部调用
     * @Param:
     * @return:
     * @Author: sky.lin
     * @Date: 2018/12/21
     */
    @GetMapping("/getPhoneByUserId")
    public String getPhoneByUserId(@Param("userId") long userId){
        return userMapper.queryPhoneByUserId(userId);
    }

    /**
     * @Description: 根据 userId查找昵称，仅内部调用
     * @Param:
     * @return:
     * @Author: sky.lin
     * @Date: 2018/12/21
     */
    @GetMapping("/getNickNameByUserId")
    public String getNickNameByUserId(@Param("userId") long userId){
        return userMapper.queryNickNameByUserId(userId);
    }
}
